/**
 * Created by meiguizhinian on 2018/7/16
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.SpineAniPlayer = ivy.BaseAniPlayer.extend({
	/**
	 * create(const AnimationID& id)
	 * create(int32_t classID,         actionID = 0, sequenceID = 0, selfUpdate = true, playState = AniPlayState::eCircle, playType = AniPlayType::eNormal)
	 * create(std::string aniFileName, actionID = 0, sequenceID = 0, selfUpdate = true, playState = AniPlayState::eCircle, playType = AniPlayType::eNormal)
	 * */
	ctor: function (ani, actionID=0, sequenceID=0, selfUpdate=true, playState=ivy.AniPlayState.eCircle, playType=ivy.AniPlayType.eNormal) {
		this._super();
	},

	getPlayerType : function() {
		return ivy.AniPlayerType.eSpine;
	},

	setExDuration : function(d){
		this._exDuration = d*2;
	},

	// SpineAniPlayer* clone(bool isHold = false)
	// {
	// 	SpineAniPlayer* aniplayerPtr = new SpineAniPlayer();
	// 	aniplayerPtr->autorelease();
	// 	aniplayerPtr->init(getSpineAnimationData(), actionID_, _playState);
	// 	return aniplayerPtr;
	// }

	resetAnimation : function(animationID) {
		let animationDataPtr = ivy.EditorDataManager.getInstance().getSpineAnimationDataBy(animationID.classID);
		if (animationDataPtr !== this._spineAnimationData) {
			let p = this.player_;
			p.setVisible(false);
			ivy.Tools.pushFunctionToCocosThread(function() {
				p.removeFromParent();
			});
			this.player_ = null;
			this._spineAnimationData = animationDataPtr;
			this.actionID_ = id.actionID;
			this._playState = id.playState;
			this.init(this.getSpineAnimationData(), this.actionID_, this._playState);
			return;
		}
		this._playState = id.playState;
		this.setActionID(id.actionID);
	},

	setPlayState : function(aniPlayState, autoResetSeqID=true) {
		this._playState = aniPlayState;
		this.setActionID(this.actionID_);
		if (this._playState===ivy.AniPlayState.eNotCircle) {
			this.setPlayOverFunction(function(){
				this._isPlayEnd = true;
			}.bind(this));
		}
	},

	reset : function() {
		this.setActionID(this._actionIDBak, true);
	},

	//checkVisibilityInGameCamera : function(transform, size)

	setActionID : function(actionID, resetSequence=true) {
		if (!resetSequence) {
			if (this.actionID_ === actionID)
				return;
		}
		this.actionID_ = actionID;
		this._actionIDBak = this.actionID_;
		if (this.player_ && this._spineAnimationData) {
			let actionName = this._spineAnimationData.getActionNameByIndex(this.actionID_);
			if (actionName !== "") {
				let entry = this.player_.setAnimation(0, actionName, this._playState !== ivy.AniPlayState.eNotCircle);
				if (entry) {
					this.player_.getState().apply(this.player_.getSkeleton());
				}
				let size = cc.size(this.getCollisionBox());
				this.setContentSize(size);
				this.player_.setContentSize(size);
			}
		}
		this._isPlayEnd = false;
	},

	getSpineAnimationData : function() {
		return this._spineAnimationData;
	},

	getActionCount : function() {
		if (this._spineAnimationData) {
			return this._spineAnimationData.getActionCount();
		}
		return 0;
	},

	getAttackBox : function() {
		if (this._spineAnimationData) {
			return this._spineAnimationData.getAttackBox(this.actionID_, 0);
		}
		return cc.rect();
	},

	setColor : function(color) {
		if (this.player_) {
			this.player_.setColor(color);
		}
	},

	getColor : function() {
		if(this.player_) {
			return this.player_.getColor();
		}
		return cc.color.WHITE;
	},

	getCollisionBox : function() {
		if (this._spineAnimationData) {
			return this._spineAnimationData.getCollisionBox(this.actionID_, 0);
		}
		return cc.rect();
	},

	setPlayOverFunction : function(func) {
		this._playOverFunc = func;
		if (this.player_) {
			this.player_.setCompleteListener(this._playOverFunc);
		}
	},

	setCustomBlendFunc : function(blendFunc) {
		this._useCustomBlendFunc = true;
		this.blendFunc_ = blendFunc;
		if (this.player_) {
			this.player_.setBlendFunc(blendFunc);
		}
	},

	setFlippedX : function(filp) {
		if (filp !== this.isFilppedX) {
			this.isFilppedX = filp;
			if (filp) {
				if (this.player_)
					this.player_.getSkeleton().flipX = 1;
			} else {
				if (this.player_)
					this.player_.getSkeleton().flipX = 0;
			}
		}
	},

	setSelfUpdate : function(up) {
		this._selfUpdate = up;
	},

	init : function() {
		if (arguments.length>0) {
			//aniData, actionID, playState
			this.actionID_ = arguments[1];
			this._actionIDBak = this.actionID_;
			this._playState = arguments[2];
			this._spineAnimationData = arguments[0];
			if (this._spineAnimationData) {
				let actionName = this._spineAnimationData.getActionNameByIndex(this.actionID_);
				if (actionName !== "") {
					let data = this._spineAnimationData.getSkeletonData();
					this.player_ = new sp.SkeletonAnimation(data, false);
					this.player_.setCascadeColorEnabled(true);
					this.player_.setCascadeOpacityEnabled(true);
					this.player_.setAnimation(0, actionName, this._playState !== ivy.AniPlayState.eNotCircle);
					this.addChild(this.player_);
					this.player_.setPosition(cc.p());
				}
			}

			this.setCascadeColorEnabled(true);
			this.setCascadeOpacityEnabled(true);
		}
		return true;
	},
	visit : function(parentCmd) {
		if (this._exDuration > 0) {
			--this._exDuration;
			if (this.player_)
				this.player_.setTimeScale(0.0);
			return;
		} else {
			if (this.player_)
				this.player_.setTimeScale(1.0);
		}

		if (!this.player_) {
			return;
		}
		cc.log("CC_USE_CULLING");
		this._super(parentCmd);
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	player_ : null,//spine::SkeletonAnimation*
	_spineAnimationData : null,//SpineAnimationData*
	_isScheduledBase : true,
	_insideBounds : true,
});

ivy.SpineAniPlayer.create = function (ani, actionID=0, sequenceID=0, selfUpdate=true, playState=ivy.AniPlayState.eCircle, playType=ivy.AniPlayType.eNormal) {
	if (ani instanceof ivy.AnimationID) {
		return this.create(ani.classID, ani.actionID, ani.sequenceID, ani.selfUpdate, ani.playState, ani.playType);
	} else {
		let animationDataPtr = null;
		if (cc.isNumber(ani)) {//classID = ani;
			animationDataPtr = ivy.EditorDataManager.getInstance().getSpineAnimationDataBy(ani);
		} else if (cc.isString(ani)) {//aniFileName = ani;
			animationDataPtr = ivy.EditorDataManager.getInstance().getSpineAnimationDataBy(ani);
		}
		if (animationDataPtr) {
			let aniplayerPtr = new ivy.SpineAniPlayer();
			aniplayerPtr.init(animationDataPtr, actionID, playState);
			aniplayerPtr.setSelfUpdate(selfUpdate);
			return aniplayerPtr;
		}
	}
	return null;
};
